package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.listen.SpecialWorkExcelListener;
import com.yearEndWorkload.mapper.*;
import com.yearEndWorkload.pojo.excel.exports.NewOutstandingExportExcel;
import com.yearEndWorkload.pojo.excel.exports.SpecialWorkExportExcel;
import com.yearEndWorkload.pojo.excel.importTemplate.SpecialWorkExcel;
import com.yearEndWorkload.pojo.form.add.AddSpecializedWorkChildForm;
import com.yearEndWorkload.pojo.form.add.AddSpecializedWorkForm;
import com.yearEndWorkload.pojo.form.update.UpdateSpecializedWorkChildForm;
import com.yearEndWorkload.pojo.form.update.UpdateSpecializedWorkForm;
import com.yearEndWorkload.pojo.query.SpecializedWorkQuery;
import com.yearEndWorkload.service.impl.SpecializedWorkServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s3/specializedWork")
@Tag(name = "专项工作绩效管理", description = "专项工作绩效管理")
public class SpecializedWorkController {
    @Autowired
    SpecializedWorkMapper specializedWorkMapper;

    @Autowired
    SpecializedChildMapper specializedChildMapper;

    @Autowired
    TypeStandardMapper typeStandardMapper;

    @Autowired
    SpecializedWorkServiceImpl specializedWorkService;

    @Operation(summary = "总-Excel批量导入专项工作绩效管理信息", description = "Excel批量导入专项工作绩效管理信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String, Object>> addBatch(@RequestParam("file") @Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {

        SpecialWorkExcelListener specialWorkExcelListener = new SpecialWorkExcelListener();
        specialWorkExcelListener.setSpecializedChildMapper(specializedChildMapper);
        specialWorkExcelListener.setSpecializedWorkMapper(specializedWorkMapper);
        specialWorkExcelListener.setTypeStandardMapper(typeStandardMapper);
        EasyExcel.read(file.getInputStream(), SpecialWorkExcel.class, specialWorkExcelListener).sheet().doRead();

        Map<String, Object> map = new HashMap<>();

        List<SpecialWorkExcel> unSave = specialWorkExcelListener.getUnSave();
        map.put("unSave", unSave);

        return Result.success(map);
    }

    @Operation(summary = "总-列表查询获取", description = "列表查询获取")
    @PostMapping("/getList")
    public Result getList(@RequestBody SpecializedWorkQuery specializedWorkQuery) {
        return specializedWorkService.getList(specializedWorkQuery);
    }

    @Operation(summary = "父-添加一个专项工作绩效管理", description = "添加一个专项工作绩效管理")
    @PostMapping("/addSpecializedWork")
    public Result addSpecializedWork(@RequestBody AddSpecializedWorkForm addSpecializedWorkForm) {
        return specializedWorkService.addSpecializedWork(addSpecializedWorkForm);
    }

    @Operation(summary = "父-更改一个专项工作绩效管理", description = "更改一个专项工作绩效管理")
    @PostMapping("/updateSpecializedWork")
    public Result updateSpecializedWork(@RequestBody UpdateSpecializedWorkForm updateSpecializedWorkForm) {
        return specializedWorkService.updateSpecializedWork(updateSpecializedWorkForm);
    }

    @Operation(summary = "父-删除多个专项工作绩效管理", description = "删除多个专项工作绩效管理")
    @DeleteMapping("/deleteSpecializedWorkByIds")
    public Result deleteByIds(@RequestBody List<String> list) {
        return specializedWorkService.deleteSpecializedWorkByIds(list);
    }

    @Operation(summary = "子-添加一个子业绩", description = "添加一个子业绩")
    @PostMapping("/addSpecializedWorkChild")
    public Result addSpecializedWorkChild(@RequestBody AddSpecializedWorkChildForm addSpecializedWorkChildForm) {
        return specializedWorkService.addSpecializedWorkChild(addSpecializedWorkChildForm);
    }

    @Operation(summary = "子-更改一个子业绩", description = "更改一个子业绩")
    @PostMapping("/updateSpecializedWorkChild")
    public Result updateSpecializedWorkChild(@RequestBody UpdateSpecializedWorkChildForm updateSpecializedWorkChildForm) {
        return specializedWorkService.updateSpecializedWorkChild(updateSpecializedWorkChildForm);
    }

    @Operation(summary = "子-删除多个子业绩", description = "删除多个子业绩")
    @DeleteMapping("/deleteSpecializedWorkChildByIds")
    public Result deleteSpecializedWorkChildByIds(@RequestBody List<String> list) {
        return specializedWorkService.deleteSpecializedWorkChildByIds(list);
    }

    @Operation(summary = "Excel导出专项工作绩效管理信息",description = "Excel导出专项工作绩效管理信息")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
        ExcelResProp.setExcelRespProp(response, "专项工作绩效管理");
        List<SpecialWorkExportExcel> specializedWorkExportExcels = specializedWorkService.getSpecializedWorkExportExcels();
        EasyExcel.write(response.getOutputStream())
                .head(SpecialWorkExportExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("专项工作绩效管理")
                .doWrite(specializedWorkExportExcels);
    }

}
